﻿@{
    Layout = "~/Views/Shared/_LayoutAdminPage.cshtml";
}
@section header{
    <style>



        [lay-filter="eQuestionForm"] .layui-form-item, .layui-inline {
            margin-bottom: 0;
            margin-top: 20px;
        }

        .qa-xx-item {
            padding-left: 25px;
            margin-bottom: 20px;
        }

        .qa-xx-item-title {
            position: absolute;
            left: 0;
            line-height: 38px;
        }
    </style>
}

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">代码生成（请谨慎操作！）</div>
        <div class="layui-card-body">
            <div class="layui-form" lay-filter="eQuestionForm" style="max-width: 960px;">
                <input type="hidden" name="questionId" value="" />
                <div class="layui-form-item" style="margin-top: 0;">
                    <div class="layui-form-item">
                        <label class="layui-form-label">数据库:</label>
                        <div class="layui-input-block">
                            <input type="radio" lay-filter="dbType" name="dbType" value="1" title="MySQL" checked>
                            <input type="radio" lay-filter="dbType" name="dbType" value="2" title="SQLServer">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">链接字符:</label>
                        <div class="layui-input-block">
                            <input name="connStr" placeholder="请输入链接字符串" class="layui-input"
                                   lay-verType="tips" lay-verify="required" required />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-block">
                            <button class="layui-btn layui-btn-normal connDbBtn">&emsp;链接&emsp;</button>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">数据表:</label>
                        <div class="layui-input-inline">
                            <select name="dbTable" class="dbTable" lay-filter="dbTable" lay-verType="tips" lay-verify="required" required>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn layui-btn-primary OutPutModelBtn" typeid="1">&emsp;预展Model&emsp;</button>
                            <button type="button" class="layui-btn layui-btn-primary OutPutModelBtn" typeid="2">生成Model文件</button>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">生成后端:</label>
                        <div class="layui-input-block">
                            <input type="text" name="ServiceName" placeholder="请输入Service名称" class="layui-input inline-block"
                                   style="width: 160px;" />
                            <button type="button" class="layui-btn layui-btn-primary OutPutServiceBtn" typeid="1">预展Service</button>
                            <button type="button" class="layui-btn layui-btn-primary OutPutServiceBtn" typeid="2">生成Service</button>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">生成前端:</label>
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn layui-btn-primary OutPutHtmlBtn">生成代码</button>
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label">预展代码:</label>
                        <div class="layui-input-block">
                            <textarea id="codeEditTool"></textarea>
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">预展代码:</label>
                        <div class="layui-input-block">
                            <textarea name="ShowCodeEdit" class="layui-textarea"></textarea>
                        </div>
                    </div>

                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block text-center">
                        <button ew-event="closeThisTabs" type="button" class="layui-btn layui-btn-primary">&emsp;关闭&emsp;</button>
                        <button class="layui-btn" lay-filter="eQuestionSubmit" lay-submit>&emsp;提交&emsp;</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="/assets/libs/tinymce/tinymce.min.js"></script>

<script>layui.use(['layer', 'form', 'xmSelect', 'layedit', 'notice'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var xmSelect = layui.xmSelect;
        var layedit = layui.layedit;
        var notice = layui.notice;

        // 渲染代码富文本编辑器
        tinymce.init({
            selector: '#codeEditTool',
            height: 525,
            branding: false,
            language: 'zh_CN',
            plugins: 'code print preview fullscreen paste searchreplace save autosave link autolink image imagetools media table codesample lists advlist hr charmap emoticons anchor directionality pagebreak quickbars nonbreaking visualblocks visualchars wordcount',
            toolbar: 'fullscreen preview code | undo redo | forecolor backcolor | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | formatselect fontselect fontsizeselect | link image media emoticons charmap anchor pagebreak codesample | ltr rtl',
            toolbar_drawer: 'sliding',
            images_upload_url: '/api/admin/UploadFile2?resultType=tinymce',
            file_picker_types: 'media',
            file_picker_callback: function (callback, value, meta) {
                layer.msg('不允许上传', { anim: 6 });
            },
            init_instance_callback: function (editor) {
                console.log(editor);
            }
        });


        function GetDbType() {
            $.get("/api/admin/code/GetDBType", { id: $('input[name="dbType"]:checked').val() }, function (res) {
                res = res.data;
                $('input[name="connStr"]').val(res);
            }, 'json');
        }

        GetDbType();

        //数据库切换事件
        form.on('radio(dbType)', function (data) {
            GetDbType();
        });

        //测试数据库链接
        $(".connDbBtn").click(function () {
            notice.msg('处理中...', { icon: 4, close: true });
            $.post("/api/admin/code/InitDBConnection", { id: $('input[name="dbType"]:checked').val(), connStr: $('input[name="connStr"]').val() }, function (res) {
                notice.destroy();
                if (res.code == 1) {
                    var option = '';
                    
                    if (res.data.length>0) {
                        for (var i = 0; i < res.data.length; i++) {
                            option += "<option value='" + res.data[i].name + "'>" + res.data[i].name + "</option>";
                        }
                        $('input[name="ServiceName"]').val(res.data[0].name + "Service");
                    }
                    $(".dbTable").html("");
                    $(".dbTable").append(option);
                    form.render('select');
                    notice.msg(res.message, { icon: 1 });
                } else {
                    notice.msg(res.message, { icon: 2 });
                }
            }, 'json');
        })

    //数据库切换事件
    form.on('select(dbTable)', function (data) {
        $('input[name="ServiceName"]').val(data.value + "Service");
    });

        //生成数据库模型
        $(".OutPutModelBtn").click(function () {
            var type = $(this).attr("typeid");
            if (type == 2) {
                parent.layui.admin.confirm('确定生成Model文件？', function (index) {
                    notice.msg('处理中...', { icon: 4, close: true });
                    $.post("/api/admin/code/OutPutModel", { id: $('input[name="dbType"]:checked').val(), type: type, tablename: $(".dbTable").val() }, function (res) {
                        notice.destroy();
                        parent.layer.close(index);
                        if (res.code == 1) {
                            notice.msg(res.message, { icon: 1 });
                        } else {
                            notice.msg(res.message, { icon: 2 });
                        }
                    }, 'json');
                });
            } else if (type == 1) {
                notice.msg('处理中...', { icon: 4, close: true });
                $.post("/api/admin/code/OutPutModel", { id: $('input[name="dbType"]:checked').val(), type: type, tablename: $(".dbTable").val() }, function (res) {
                    notice.destroy();
                    if (res.code == 1) {
                        notice.msg(res.message, { icon: 1 });
                        tinymce.get('codeEditTool').setContent("<pre>" + res.data + "</pre>");
                        $('textarea[name="ShowCodeEdit"]').val(res.data);
                    } else {
                        notice.msg(res.message, { icon: 2 });
                    }
                    
                }, 'json');
            }
        });


    //生成Service
    $(".OutPutServiceBtn").click(function () {
        var type = $(this).attr("typeid");
        if (type == 2) {
            parent.layui.admin.confirm('确定生成Service文件？', function (index) {
                notice.msg('处理中...', { icon: 4, close: true });
                $.post("/api/admin/code/OutPutService", { id: $('input[name="dbType"]:checked').val(), type: type, tablename: $(".dbTable").val(), servicename: $('input[name="ServiceName"]').val()}, function (res) {
                    notice.destroy();
                    parent.layer.close(index);
                    if (res.code == 1) {
                        notice.msg(res.message, { icon: 1 });
                    } else {
                        notice.msg(res.message, { icon: 2 });
                    }
                }, 'json');
            });
        } else if (type == 1) {
            notice.msg('处理中...', { icon: 4, close: true });
            $.post("/api/admin/code/OutPutService", { id: $('input[name="dbType"]:checked').val(), type: type, tablename: $(".dbTable").val(), servicename: $('input[name="ServiceName"]').val()}, function (res) {
                notice.destroy();
                if (res.code == 1) {
                    notice.msg(res.message, { icon: 1 });
                    tinymce.get('codeEditTool').setContent("<pre>" + res.data + "</pre>");
                    $('textarea[name="ShowCodeEdit"]').val(res.data);
                } else {
                    notice.msg(res.message, { icon: 2 });
                }
               
            }, 'json');
        }
    });

    $(".OutPutHtmlBtn").click(function () {
        notice.msg('处理中...', { icon: 4, close: true });
        $.post("/api/admin/code/OutPutHtml", { id: $('input[name="dbType"]:checked').val(),  tablename: $(".dbTable").val()}, function (res) {
            notice.destroy();
            if (res.code == 1) {
                notice.msg(res.message, { icon: 1 });
                $('textarea[name="ShowCodeEdit"]').val(res.data);
            } else {
                notice.msg(res.message, { icon: 2 });
            }
        }, 'json');
    });

    /*用正则表达式实现html转码*/
    function htmlEncodeByRegExp(str) {
        var s = "";
        if (str.length == 0) return "";
        s = str.replace(/&/g, "&");
        s = s.replace(/</g, "<");
        s = s.replace(/>/g, ">");
        s = s.replace(/ /g, " ");
        s = s.replace(/\'/g, "'");
        s = s.replace(/\"/g, "\"");
         return s;
    }
    /*用正则表达式实现html解码*/
    function htmlDecodeByRegExp(str) {
        var s = "";
        if (str.length == 0) return "";
        s = str.replace(/&/g, "&");
        s = s.replace(/</g, "<");
        s = s.replace(/>/g, ">");
        s = s.replace(/ /g, " ");
        s = s.replace(/'/g, "\'");
        s = s.replace(/"/g, "\"");
        return s;
    }



    });</script>